package com.view

import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.graphics.Shader
import android.graphics.SweepGradient
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.LinearInterpolator

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/9/27
 */
class CircleLoading : View {

    private val TAG = "CircleLoading"
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private val strokeWidth = 50f

    private val rect = RectF()
    private val startColor = 0x00000000.toInt()
    private val endColor = 0xffe60012.toInt()

    private val colors = intArrayOf(startColor, endColor)

    private var viewWidth = 0
    private var viewHeight = 0

    /**** 动画配置 ****/
    private var isAnimator = false
    private var animator: ValueAnimator? = null
    private var animatorDuration = 10000L

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        startAnimation()
    }

    init {
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = strokeWidth
        paint.strokeCap = Paint.Cap.ROUND
        paint.strokeJoin = Paint.Join.ROUND

        circlePaint.color = endColor
        circlePaint.style = Paint.Style.FILL
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        if (w != 0) {
            viewWidth = w
        }
        if (h != 0) {
            viewHeight = h
        }

        if (viewHeight != viewWidth) {
            Log.d(TAG, "宽高不一样")
        }
        val shader: Shader = SweepGradient(viewWidth / 2f, viewHeight / 2f, colors, null)
        rect.set(strokeWidth / 2, strokeWidth / 2, viewWidth - strokeWidth / 2, viewHeight - strokeWidth / 2)
        paint.shader = shader
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.let {
            it.drawArc(rect, 0f, 360f, false, paint)
            it.drawCircle(viewWidth - strokeWidth / 2, viewHeight / 2f, strokeWidth / 2, circlePaint)
        }
    }

    private fun startAnimation() {
        if (isAnimator) {
            return
        }
        isAnimator = true
        if (animator == null) {
            val animatorTemp = ValueAnimator.ofFloat(0f, 360f)
            animatorTemp.repeatCount = ValueAnimator.INFINITE
            animatorTemp.duration = animatorDuration
            animatorTemp.interpolator = LinearInterpolator()
            animatorTemp.addUpdateListener {
                rotation = it.animatedValue as Float
            }

            animator = animatorTemp
        }

        animator?.start()
    }


    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        if (animator!=null){
            startAnimation()
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stopAnimation()
    }

    private fun stopAnimation() {
        if (isAnimator) {
            return
        }
        isAnimator = false
        animator?.let {
            it.cancel()
        }
    }

}